<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Config;

class Product extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function _initialize(){
        parent::_initialize();

        $banner = Db::name('banner')->where([
            'lanmu' => 'product'
        ])->select();
        $this->assign('banner', $banner);

        $series = Db::name('product_series')->order('weigh asc,id asc')->select();
        $this->assign('series',$series);
    }

    //年度代表色
    public function index()
    {
        $cate_id = input('cate_id/d','');
        $this->assign('cate_id',$cate_id);

        $series_id = input('series_id/d','');
        $this->assign('series_id',$series_id);

        $where = [];
        if($cate_id){
            $where['cate_id'] = $cate_id;
            $cate_name = Db::name('product_cate')->where([
                'id' => $cate_id
            ])->value('title');
            $this->assign('cate_name',$cate_name);
        }else{
            $this->assign('cate_name','');
        }
        if($series_id){
            $where['series_id'] = $series_id;
        }

        $data = Db::name('product')->where($where)->order('weigh desc,id desc')->paginate(6);
        $page = $data->render();
        $this->assign('page',$page);
        $this->assign('product', $data);
        return $this->view->fetch();
    }

    public function detail()
    {
        $id = input('id/d','');

        $hots = Db::name('product')->where([
            'hot_switch' => 1,
        ])->order('weigh desc,id asc')->limit(4)->select();
        $this->assign('hots',$hots);

        $data = Db::name('product')->where('id',$id)->find();
        $this->assign('detail',$data);

        if($data['cate_id']){
            $cate_name = Db::name('product_cate')->where([
                'id' => $data['cate_id']
            ])->value('title');
            $this->assign('cate_name',$cate_name);
        }else{
            $this->assign('cate_name','');
        }

        if($data['series_id']){
            $series_name = Db::name('product_series')->where([
                'id' => $data['series_id']
            ])->value('title');
            $this->assign('series_name',$series_name);
        }else{
            $this->assign('series_name','');
        }

        return $this->view->fetch();
    }


}
